package com.woniuxy.rms.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.rms.advice.LogPoint;
import com.woniuxy.rms.node.PhoneCode;
import com.woniuxy.rms.pojo.Admins;
import com.woniuxy.rms.pojo.Roles;
import com.woniuxy.rms.service.AdminService;
import com.woniuxy.rms.service.RoleService;
import com.woniuxy.rms.util.DataResult;
import com.woniuxy.rms.util.RegexUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/29 0029:下午 11:43  <br/>
 * Description:管理员控制层
 */
@Api(description = "管理员管理")
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    RoleService roleService;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * Description : 查询管理员信息  <br/>
     * ChangeLog : 1. 创建 (2020/3/29 0029 下午 11:47 [Administrator]);
     * @param currentPage
     * @param pageSize
     * @return java.util.List<com.woniuxy.rms.pojo.Admins>
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询管理员信息",notes = "根据页码和数据条数分页查询")
    @ResponseBody
    @LogPoint("查询管理员信息")
    @RequiresPermissions("admin:select")
    public DataResult list(@ApiParam(name = "currentPage",value = "当前页码")
                           @RequestParam(defaultValue = "1",required = false,name = "page") Integer currentPage,
                           @ApiParam(name = "pageSize",value = "每页数据条数")
                           @RequestParam(defaultValue = "10",required = false,name = "limit") Integer pageSize){
        List<Admins> admins = adminService.findByPage(currentPage,pageSize);
        PageInfo info = new PageInfo(admins);
        return DataResult.success(info);
    }
    /**
     * Description : 给管理员授予角色  <br/>
     * ChangeLog : 1. 创建 (2020/3/30 0030 上午 12:00 [Administrator]);
     * @param admins
     * @return com.woniuxy.rms.util.DataResult
    */
    @ResponseBody
    @PutMapping("/editAdminRole")
    @LogPoint("更改管理员角色")
    @ApiOperation(value = "更改管理员角色",notes = "根据管理员id进行更改,只需要传入管理员id和角色role_id")
    @RequiresPermissions("admin:update")
    public DataResult editAdminRole(@ApiParam(name = "admins",value = "管理员对象",required = true) Admins admins){
        return adminService.editAdminRole(admins);

    }
    /**
     * Description : 跳转到编辑角色页面  <br/>
     * ChangeLog : 1. 创建 (2020/3/30 0030 上午 12:06 [Administrator]);
     * @param id
     * @param modelAndView
     * @return org.springframework.web.servlet.ModelAndView
    */
    @GetMapping("/adminRole")
    @ApiOperation(value = "跳转到更改管理员角色页面",notes = "根据管理员id跳转到指定管理员的编辑页面")
    public ModelAndView adminRole(@ApiParam(name = "id",value = "管理员id",required = true) Long id,ModelAndView modelAndView){
        modelAndView.setViewName("/system/adminRole");
        //查询所有角色
        List<Roles> roles = roleService.findAll();
        //查询管理员信息
        Admins admin = adminService.findById(id);
        modelAndView.addObject("roles",roles);
        modelAndView.addObject("admin",admin);
        return modelAndView;
    }
    /**
     * Description : 删除管理员  <br/>
     * ChangeLog : 1. 创建 (2020/4/8 0008 下午 12:44 [Administrator]);
     * @param id
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/delete")
    @ResponseBody
    public DataResult delete(Long id){
        return adminService.delete(id);
    }
    /**
     * Description : 跳转到个人资料页面  <br/>
     * ChangeLog : 1. 创建 (2020/3/31 0031 上午 10:14 [Administrator]);
     * @param modelAndView
     * @return org.springframework.web.servlet.ModelAndView
    */
    @GetMapping("/personal")
    public ModelAndView personal(ModelAndView modelAndView){
        modelAndView.setViewName("/system/personal");
        //获取管理员资料
        Admins admin = (Admins) SecurityUtils.getSubject().getPrincipal();
        admin = adminService.findById(admin.getId());
        modelAndView.addObject("admin",admin);
        return modelAndView;

    }
    /**
     * Description : 修改个人资料  <br/>
     * ChangeLog : 1. 创建 (2020/3/31 0031 上午 10:20 [Administrator]);
     * @param admins
     * @return com.woniuxy.rms.util.DataResult
    */
    @PutMapping("/updateAdmin")
    @ResponseBody
    @ApiOperation(value = "修改管理员个人资料",notes = "根据管理员id修改管理员资料")
    public DataResult updateAdmin(@ApiParam(name = "admins",value = "管理员对象",required = true) Admins admins){
        return adminService.updateAdmin(admins);
    }
    /**
     * Description : 密码重置  <br/>
     * ChangeLog : 1. 创建 (2020/3/31 0031 上午 10:56 [Administrator]);
     * @param admins
     * @return com.woniuxy.rms.util.DataResult
    */
    @PostMapping("/changePwd")
    @ResponseBody
    @ApiOperation(value = "重置用户密码")
    public DataResult changePwd(@ApiParam(name = "admins",value = "管理员对象，只需传入重置密码、用户id、验证码即可") Admins admins){
        Admins admin = (Admins) SecurityUtils.getSubject().getPrincipal();
        admins.setId(admin.getId());
        admins.setAccount(admin.getAccount());
        try {
            String code = (String) redisTemplate.opsForValue().get(admin.getAccount()+":code");
            return adminService.changePwd(admins,code);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.failed("验证码已失效！");
        }

    }

    /**
     * Description : 发送短信验证码  <br/>
     * ChangeLog : 1. 创建 (2020/3/29 0029 下午 4:33 [Administrator]);
     * @return com.woniuxy.rms.util.DataResult
     */
    @GetMapping("/node")
    @ResponseBody
    @ApiOperation(value = "获取短信验证码")
    public DataResult getNode(){
        //手机号验证
        Admins admins = (Admins) SecurityUtils.getSubject().getPrincipal();
        admins = adminService.findById(admins.getId());
        //获取随机验证码并发送
        DataResult code = PhoneCode.getPhonemsg(admins.getPhone());
        if (code.getCode() == 200){
            redisTemplate.opsForValue().set(admins.getAccount()+":code",code.getData());//将验证码存进redis中
            redisTemplate.expire(admins.getAccount()+":code",5, TimeUnit.MINUTES);//有效时间为5分钟
            return DataResult.success("获取验证码成功！");
        }
        return DataResult.failed("获取验证码失败！");

    }
}